package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.domain.vo.SellerVO;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.service.SellerService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("seller")
@CrossOrigin  //跨域
@Api("商家后台管理")
@RefreshScope   //自动监听配置文件
@Slf4j
public class SellerController {

    @Autowired
    private SellerService sellerService;

    @PostMapping("sellerByName")
    public Result sellerByName(@RequestParam String sellerId) {
        SellerVO sellerVO = sellerService.sellerByName(sellerId);
        return Result.OK().setData(sellerVO);
    }

    //注册
    @PostMapping("sellerAdd")
    public Result sellerAdd(@RequestBody SellerVO vo) {
        try {
            sellerService.sellerAdd(vo);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ADD_NAME_REPEAT);
        }
    }

    //查询全部
    @PostMapping("sellerList")
    public Result sellerList() {
        List<SellerVO> sellerVOList = sellerService.sellerList();
        return Result.OK().setData(sellerVOList);
    }

    //更改商家的状态
    @PostMapping("updateState")
    public Result updateState(@RequestBody String sellerId, @RequestParam int state) {
        sellerService.updateState(sellerId,state);
        return Result.OK();
    }

}
